async function log_in() {
    const askele = document.getElementById("notes-entry-div");
    const loadele = document.getElementById("notes-loading");
    const resultele = document.getElementById("notes-msg");
    const ent = document.getElementById("notes-entry");
    let uname = ent.value.trim(); // 去除输入的空格

    askele.style.display = "none";
    loadele.style.display = "block";

    const messages = {
        "WHZ": "你的海滩生活如何呀？有没有去冲浪？有没有游泳？<del>有没有写完作业？有没有积累作文素材？</del>Bi--"
    };

    if (uname in messages) {
        resultele.innerHTML = `<p>${messages[uname]}</p>`;
    } else {
        resultele.innerHTML = "<p>新年快乐！大家寒假作业写完了没有呀？<span class='smallText answers'>（我没有……）</span></p>";
    }

    const refreshbut = document.getElementById("refreshButton") || document.createElement("button");
    refreshbut.innerHTML = "退出登录";
    refreshbut.id = "refreshButton";
    refreshbut.onclick = function () { 
        resultele.innerHTML = "";
        resultele.style.display = "none";
        askele.style.display = "block";
        loadele.style.display = "none";
        ent.value = ""; 
    };
    resultele.appendChild(refreshbut);

    await waitFor(1.5);
    loadele.style.display = "none";
    resultele.style.display = "block";
}
function keyup_submit(e) {
    if (document.getElementById("notes-entry").focus) {
        let evt = window.event || e;
        if (evt.keyCode == 13) {
            log_in();
        }
    }
}
function waitFor(seconds) {
    return new Promise(resolve => setTimeout(resolve, seconds * 1000));
}
function show_wmq() {
    const pwdent = document.getElementById("wmq-entry");
    const pwddiv = document.getElementById("wmq-entry-div");
    const wmqcon = document.getElementById("wmq-content");
    let passwd = pwdent.value.trim();
    if(passwd != "稳如泰山") {
        alert("间谍！哪里跑！不许猜我们的口号！\n哦，如果你只是写错了，欢迎你再试一次。");
    }else{
        pwddiv.style.display = "none";
        wmqcon.style.display = "block";
    }
}
function keyup_submit_wmq(e) {
    if (document.getElementById("wmq-entry").focus) {
        let evt = window.event || e;
        if (evt.keyCode == 13) {
            show_wmq();
        }
    }
}
function toTop() {
      // 返回顶部,添加过渡动画，如果下拉距离超过10000，就统一0.8秒回到顶部，否则按默认速度
      // 如果想要过渡事件长一点就把下面的80加大，否则减小
      let scrollTo = document.documentElement.scrollTop;
      if (this.myTimer == -1) {
        if (scrollTo > 10000) {
          let num = (scrollTo / 80).toFixed(0);
          this.myTimer = setInterval(() => {
            scrollTo -= num;
            if (scrollTo <= 0) {
              scrollTo = 0;
              window.clearInterval(this.myTimer);
              this.myTimer = -1;
            }
            window.scrollTo(0, scrollTo);
          }, 10);
        } else {
          this.myTimer = setInterval(() => {
            scrollTo -= 100;
            if (scrollTo <= 0) {
              scrollTo = 0;
              window.clearInterval(this.myTimer);
              this.myTimer = -1;
            }
            window.scrollTo(0, scrollTo);
          }, 10);
        }
      }
}
this.myTimer = -1;
// function toTop(){window.scrollTo(0,0);}
function requestFullScreen(element) {
    var requestMethod = element.requestFullScreen || element.webkitRequestFullScreen || element.mozRequestFullScreen || element.msRequestFullScreen;
    if (requestMethod) {
        requestMethod.call(element);
        var el = document.getElementById("toggleFullScreen");
        el.title = "点击以退出沉浸式全屏阅读。";
        el.onclick = function(){
            cancelFullScreen();
        }
        el = document.getElementById("toggleFullScreenImg");
        el.className = "fa fa-compress";
        console.log("User action:Into full-screen using <button>");
    } else if (typeof window.ActiveXObject !== "undefined") {
        var wscript = new ActiveXObject("WScript.Shell");
        if (wscript !== null) {
            wscript.SendKeys("{F11}");
            var el = document.getElementById("toggleFullScreen");
            el.title = "点击以退出沉浸式全屏阅读。";
            el.onclick = function(){
                cancelFullScreen();
            }
            el = document.getElementById("toggleFullScreenImg");
            el.className = "fa fa-compress";
            console.log("User action:(IE mode)Into full-screen using <button> with ActiveXObject");
        }
    }else{
        alert("抱歉,您的浏览器不支持全屏。请按F11键进入全屏。");
        console.log("%cUser action:Into full-screen using <button>,but action failed due to browser reason,time:" + toString(new Date().getTime()),"color:red");
    }
}
function cancelFullScreen(){
    var requestMethod = document.exitFullscreen || document.webkitCancelFullScreen || document.webkitExitFullScreen;
    if(requestMethod){
        requestMethod.call(document);
        var el = document.getElementById("toggleFullScreen");
        el.title = "点击以进入沉浸式全屏阅读。";
        el.onclick = el.onclick = function(){
            requestFullScreen(document.documentElement);
        }
        el = document.getElementById("toggleFullScreenImg");
        el.className = "fa fa-expand";
        console.log("User action:Exit full-screen using <button>");
        // alert("您已退出沉浸式阅读模式。");
    }else{
        alert("抱歉,您的浏览器无法退出全屏。请按F11键退出全屏。");
        console.log("%cUser action:Exit full-screen using <button>,but action failed due to browser reason,time:" + toString(new Date().getTime()),"color:red");
    }
}
function toggle_light_dark(){
    let tHtml = document.documentElement;
    const cur = tHtml.classList;
    if(!cur.contains("darkTheme")){
        tHtml.classList.add("darkTheme");
    }else{
        tHtml.classList.remove("darkTheme");
    }
}

/*判断深浅主题*/
const cur_scheme = window.matchMedia("(prefers-color-scheme: dark)")
if(cur_scheme.matches){document.documentElement.classList.add("darkTheme");}

function copycodes(event){
    return 0;
}

document.addEventListener('DOMContentLoaded', (event) => {
    const codes = document.querySelectorAll('code');
    codes.forEach((code, index) => {
        const button = document.createElement('button');
        button.innerHTML = '<i class="fa fa-copy"></i> 复制';
        button.className = 'codeCopyBut';
        code.appendChild(button); // 将按钮添加到<code>标签内
        button.onclick = function(){
            navigator.clipboard.writeText(code.innerText).then(async function(){
                button.innerHTML = '<i class="fa fa-check"></i> 已复制';
                button.style.color = '#29a66a';
                await waitFor(1.5);
                button.style.removeProperty('color');
                button.innerHTML = '<i class="fa fa-copy"></i> 复制';
            }).catch(err => async function(){
                console.error('Failed to copy!', err);
                button.innerHTML = '<i class="fa fa-times"></i> 复制失败';
                button.style.color = 'red';
                await waitFor(2);
                button.style.removeProperty('color');
                button.innerHTML = '<i class="fa fa-copy"></i> Copy Code';
            });
        };
    });
});
